<?php
if (! defined ( 'BASEPATH' ))
    exit ( 'No direct script access allowed' );
class Pay extends CI_Controller {
    public function __construct() {
        parent::__construct ();
        // 配置加载支付信息
        $this->load->config ( "config_pay" );
    }
    public function index() {
        $this->load->library ( "pay/merchant_order" );
        $this->load->library ( 'pay/yeepay' );
        
        // 检查支付信息是否合法
        $info = $this->merchant_order->check_order_info ();
        if ($info ['result'] == false) {
            $this->load->view ( "pay/pay_error", $info );
            return;
        }
        // 生成支付订单
        $order_info = $this->merchant_order->make_order ();
        if ($order_info ['result'] == false) {
            $this->load->view ( "pay/pay_error", $order_info );
            return;
        }
        
        $this->load->library ( "encrypt" );
        $data ['encrypt_tran_id'] = $this->encrypt->encode ( $order_info ['tran_id'] );
        
        // 跳转选择支付方式,避免由于刷新界面造成的重复提交
        switch ($this->merchant_order->get_pay_type ()) {
            case 'yeepay' :
                $card_denomination = $this->input->get_post ( "money" );
                if ($card_denomination == "") {
                    $card_denomination = $this->yeepay->get_price ();
                }
                redirect ( base_url () . "index.php/pay/pay_order?encrypt_tran_id=" . $data ['encrypt_tran_id'] . "&pay_type=1&Price=" . $this->yeepay->get_price () . "&card_no=" . $this->yeepay->get_card_no () . "&card_password=" . $this->yeepay->get_card_password () . "&t_card_type=" . $this->yeepay->get_card_type () . "&AccountNo=" . $this->merchant_order->get_account_no () . "&money=" . $card_denomination );
                break;
            case 'alipay' :
                redirect ( base_url () . "index.php/pay/pay_order?encrypt_tran_id=" . $data ['encrypt_tran_id'] . "&pay_type=2&AccountNo=" . $this->merchant_order->get_account_no () );
                break;
            case 'tenpay' :
                redirect ( base_url () . "index.php/pay/pay_order?encrypt_tran_id=" . $data ['encrypt_tran_id'] . "&pay_type=3&AccountNo=" . $this->merchant_order->get_account_no () );
                break;
            case 'upmp' :
                redirect ( base_url () . "index.php/pay/pay_order?encrypt_tran_id=" . $data ['encrypt_tran_id'] . "&pay_type=4&AccountNo=" . $this->merchant_order->get_account_no () );
                break;
        }
    }
    
    /**
     * 跳转到支付订单
     */
    public function pay_order() {
        $data ['encrypt_tran_id'] = $this->input->get_post ( "encrypt_tran_id" );
        $this->load->library ( "encrypt" );
        $tran_id = $this->encrypt->decode ( $data ['encrypt_tran_id'] );
        $this->load->library ( "pay/merchant_order" );
        $tran_info = $this->merchant_order->get_transaction_info ( $tran_id );
        if ($tran_info == null) {
            $data ['tran_id'] = $data ['encrypt_tran_id'];
            $data ['back_url'] = base_url () . "index.php/pay/choose_pay_type?encrypt_tran_id=" . $data ['encrypt_tran_id'];
            $data ['message'] = "您要支付的订单不存在。";
            $this->load->view ( "pay/pay_error", $data );
            return;
        }
        
        if ($tran_info ['PAY_STATUS'] != 0) {
            $data ['tran_id'] = $data ['encrypt_tran_id'];
            $data ['back_url'] = base_url () . "index.php/pay/choose_pay_type?encrypt_tran_id=" . $data ['encrypt_tran_id'];
            $data ['message'] = "订单已支付。";
            $this->load->view ( "pay/pay_error", $data );
            return;
        }
        
        $account_no = $this->input->get_post ( 'AccountNo' );
        $this->merchant_order->update_order_payer ( $tran_id, $account_no );
        $pay_type = $this->input->get_post ( "pay_type" );
        
        $this->load->library ( "recharge/account_recharge" );
        $insert ['money'] = $tran_info ['MONEY'];
        $insert ['pay_type'] = $pay_type;
        $insert ['created_by'] = $account_no;
        $insert ['account_no'] = $account_no;
        $insert ['tran_id'] = $tran_id;
        $recharge_sn = $this->account_recharge->insert_recharge ( $insert );
        
        // 电话卡
        if ($pay_type == 1) {
            $this->load->library ( 'pay/yeepay' );
            $yeepay ['card_no'] = $this->yeepay->get_card_no ();
            $yeepay ['card_password'] = $this->yeepay->get_card_password ();
            $yeepay ['t_card_denomination'] = $this->yeepay->get_card_denomination ();
            $yeepay ['t_card_type'] = $this->yeepay->get_card_type ();
            $yeepay ['order_sn'] = $recharge_sn;
            $yeepay ['amount'] = $this->yeepay->get_price ();
            $yeepay ['return_url'] = base_url () . "index.php/pay_order_return/get_yeepay_return";
            
            $return = $this->yeepay->yeepay ( $yeepay );
            
            if ($return != "success") {
                $data ['message'] = $return;
                $data ['back_url'] = base_url () . "merchant_proxy/choose_pay_type?encrypt_tran_id=" . $data ['encrypt_tran_id'];
                $this->load->view ( "pay/pay_error", $data );
                return;
            }
            $data ['order_id'] = $tran_info ['ORDER_ID'];
            $data ['product_name'] = $tran_info ['PRODUCT_NAME'];
            $data ['money'] = $tran_info ['MONEY'];
            $data ['created_time'] = $tran_info ['CREATED_TIME'];
            $this->load->view ( "yeepay/yeepayprocess", $data );
        }
        
        // 支付宝
        if ($pay_type == 2) {
            $this->load->library ( "pay/alipay" );
            $alipay ['subject'] = $tran_info ['PRODUCT_NAME'];
            $alipay ['out_trade_no'] = $recharge_sn;
            $alipay ['total_fee'] = $tran_info ['MONEY'];
            $alipay ['out_user'] = $account_no;
            $alipay ['call_back_url'] = base_url () . "index.php/pay_order_return/get_alipay_return";
            $alipay ['notify_url'] = base_url () . "index.php/pay_order_return/get_alipay_notify";
            $return = $this->alipay->alipay ( $alipay );
        }
        // 财付通
        if ($pay_type == 3) {
            $this->load->library ( "pay/tenpay" );
            $tenpay ['body'] = $tran_info ['PRODUCT_NAME'];
            $tenpay ['out_trade_no'] = $recharge_sn;
            $tenpay ['total_fee'] = ($tran_info ['MONEY'] * 100);
            $tenpay ['return_url'] = base_url () . "index.php/pay_order_return/get_tenpay_return";
            $tenpay ['notify_url'] = base_url () . "index.php/pay_order_return/get_tenpay_notify";
            $return = $this->tenpay->pay ( $tenpay );
        }
        // 银联
        if ($pay_type == 4) {
            $this->load->library ( "pay/upmp" );
            $upmp ['body'] = $tran_info ['PRODUCT_NAME'];
            $upmp ['out_trade_no'] = $recharge_sn;
            $upmp ['total_fee'] = $tran_info ['MONEY'];
            $upmp ['orderTime'] = $tran_info ['CREATED_TIME'];
            $upmp ['return_url'] = base_url () . "index.php/pay_order_return/get_upmp_return";
            $upmp ['notify_url'] = base_url () . "index.php/pay_order_return/get_upmp_notify";
            $return = $this->upmp->pay ( $upmp );
        }
    }
    public function get_telephone_card_pay_result() {
        $this->load->library ( "encrypt" );
        $encrypt_tran_id = $this->input->get_post ( "encrypt_tran_id" );
        $tran_id = $this->encrypt->decode ( $encrypt_tran_id );
        
        if ($tran_id == "") {
            $data ['result'] = false;
            $data ['message'] = "您要支付的订单不存在";
            echo json_encode ( $data );
            exit ();
        }
        $this->load->library ( "pay/merchant_order" );
        $transaction_info = $this->merchant_order->get_transaction_info ( $tran_id );
        $data ['productName'] = $transaction_info ['PRODUCT_NAME'];
        $data ['productMoney'] = $transaction_info ['MONEY'];
        $data ['orderId'] = $transaction_info ['ORDER_ID'];
        $data ['payStatusCode'] = $transaction_info ['PAYMENT_PAY_STATUS'];
        
        if ($transaction_info ['PAYMENT_PAY_STATUS'] === '0') {
            $data ['result'] = true;
            $data ['url'] = base_url () . "index.php/pay/pay_success?encrypt_tran_id=" . $encrypt_tran_id;
            echo json_encode ( $data );
            exit ();
        } elseif ($transaction_info ['PAYMENT_PAY_STATUS'] == "") {
            $data ['result'] = true;
            $data ['url'] = "";
            echo json_encode ( $data );
            exit ();
        } else {
            // 支付失败 ,获取支付错误配置文案
            $this->load->config ( 'config_yeepay_error' );
            $payment_error_msg = $this->config->item ( 'yeepay_card_error' . $transaction_info ['PAYMENT_PAY_STATUS'] );
            $data ['result'] = false;
            $data ['message'] = "支付失败,失败信息:" . $payment_error_msg;
            echo json_encode ( $data );
            exit ();
        }
    }
    public function pay_success() {
        $encrypt_tran_id = $this->input->get_post ( "encrypt_tran_id" );
        $this->load->library ( "encrypt" );
        $tran_id = $this->encrypt->decode ( $encrypt_tran_id );
        $this->load->library ( "pay/merchant_order" );
        $transaction_info = $this->merchant_order->get_transaction_info ( $tran_id );
        
        /**
         * 加密返回地址的URL
         */
        $url_string = "MerId=" . $transaction_info ['MERCHANT_ID'];
        $url_string = $url_string . "&OrderId=" . $transaction_info ['ORDER_ID'];
        $url_string = $url_string . "&Money=" . $transaction_info ['MONEY'];
        $url_string = $url_string . "&TranCode=" . $transaction_info ['TRAN_CODE'];
        $url_string = $url_string . "&EncString=" . md5 ( $transaction_info ['MERCHANT_ID'] . $transaction_info ['ORDER_ID'] . $transaction_info ['MONEY'] . $transaction_info ['MERCHANT_KEY'] );
        $url_string = $url_string . "&PaymentFee=" . $transaction_info ['PAYMENT_FEE'];
        $url_string = $url_string . "&Note=" . $transaction_info ['NOTE'];
        $url_string = $url_string . "&PaymentStatusCode=" . $transaction_info ['PAYMENT_PAY_STATUS'];
        if ($transaction_info ['RETURN_URL']) {
            $return_url = $transaction_info ['RETURN_URL'] . "?" . $url_string;
        } else {
            $return_url = $transaction_info ['SERVER_NOTIFY_URL'] . "?" . $url_string;
        }
        
        /**
         * curl抓去return_url的数据
         */
        $ch = curl_init ();
        $timeout = 5;
        curl_setopt ( $ch, CURLOPT_URL, $return_url );
        curl_setopt ( $ch, CURLOPT_POST, 1 );
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
        $contents = curl_exec ( $ch );
        curl_close ( $ch );
        $contents = strtoupper ( substr ( trim ( $contents ), 0, 7 ) );
        if ($contents == "SUCCESS") {
            $this->load->model ( 'mpay_transaction_model' );
            $this->mpay_transaction_model->change_transaction_retrun ( $tran_id, 1 );
        }
        $data ['orderId'] = $transaction_info ['ORDER_ID'];
        $data ['productMoney'] = $transaction_info ['MONEY'];
        $data ['payStatusCode'] = $transaction_info ['PAYMENT_PAY_STATUS'];
        $this->load->view ( "pay/pay_success", $data );
    }
}


/*
 * End of file 'pay.php';
 * Location: './mpay/controllers/pay.php';
 **/
